const UserModel = require("../../models/UserModel")




const UserService = {
    login: async ({ username, password }) => {
        // 在模型层查找
        return UserModel.find({
            username,
            password
        })
    },
    upload: async ({ _id, username, introduction, gender, avatar }) => {
        // 前面是查询条件，后面是更新的数据
        if (avatar) {
            return UserModel.updateOne({ _id }, { username, introduction, gender, avatar })
        } else {
            return UserModel.updateOne({ _id }, { username, introduction, gender })
        }
    },
    add: async ({ username, introduction, gender, avatar, password, role }) => {
        try {
            return UserModel.create({
                username, introduction, gender, avatar, password, role
            })
        } catch (error) {
            throw err;
        }

    },
    getList: async ({ id }) => {
        return id ? UserModel.find({ _id: id }, ["username", "role", "introduction", "gender", "password"]) : UserModel.find({}, ["username", "role", "introduction", "gender", "avatar"])
    },
    delList: async ({ _id }) => {
        try {
            return UserModel.deleteOne({ _id })
        } catch (error) {
            throw err;
        }

    },
    putList: async (body) => {
        try {
            return UserModel.updateOne({ _id: body._id }, body)
        } catch (error) {
            throw err;
        }

    }
}



module.exports = UserService

